# 在许多情况下，将这些组件的状态保存到磁盘并在以后重新加载它们是非常有用的。
# 这在无状态端点响应请求并需要从持久存储中加载应用程序状态的Web应用程序中特别有用。

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.base import TaskResult
from autogen_agentchat.conditions import TextMentionTermination, ExternalTermination, MaxMessageTermination
from autogen_agentchat.messages import TextMessage
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(model="qwen2.5:7b",
                                          model_info={
                                              "vision": False,
                                              "function_calling": True,
                                              "family": "Qwen3",
                                              "structured_output": True,
                                              "json_output": True,
                                          },
                                          api_key="ollama",
                                          base_url="http://127.0.0.1:11434/v1")

async def main():
    assistant_agent = AssistantAgent(
        name="assistant_agent",
        system_message="You are a helpful assistant",
        model_client=model_client,
    )
    await Console(
        assistant_agent.on_messages_stream(
            [TextMessage(content="Write a 3 line poem on lake tangayika", source="user")],
            cancellation_token=CancellationToken(),
        )
    )
    agent_state = await assistant_agent.save_state()
    print("-"*50)
    print(agent_state)

    # 新建一个相同的代理
    assistant_agent = AssistantAgent(
        name="assistant_agent",
        system_message="You are a helpful assistant",
        model_client=model_client,
    )
    # 加载状态
    await assistant_agent.load_state(agent_state)
    await Console(
        assistant_agent.on_messages_stream(
            [TextMessage(content="Write a 3 line poem on lake tangayika", source="user")],
            cancellation_token=CancellationToken(),
        )
    )



if __name__ == "__main__":
    import asyncio
    asyncio.run(main())
